package xyz.aiyouv.aiutool.database.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;

/**
 * 数据库转Java配置信息
 *
 * @author Aiyouv
 */

@Component
@ConfigurationProperties(prefix = "aiu.database")

public class Table2JavaProperties {

    /**
     * 数据库驱动
     **/
    private String drive;
    private String entityPackage;
    private String servicePackage;
    private String controllerPackage;
    private String repositoryPackage;
    private String outFilePath;
    private String basePackage;

    public String getDrive() {
        return drive;
    }

    public void setDrive(String drive) {
        this.drive = drive;
    }

    public String getEntityPackage() {
        return entityPackage;
    }

    public void setEntityPackage(String entityPackage) {
        this.entityPackage = entityPackage;
    }

    public String getServicePackage() {
        return servicePackage;
    }

    public void setServicePackage(String servicePackage) {
        this.servicePackage = servicePackage;
    }

    public String getControllerPackage() {
        return controllerPackage;
    }

    public void setControllerPackage(String controllerPackage) {
        this.controllerPackage = controllerPackage;
    }

    public String getRepositoryPackage() {
        return repositoryPackage;
    }

    public void setRepositoryPackage(String repositoryPackage) {
        this.repositoryPackage = repositoryPackage;
    }

    public String getOutFilePath() {
        return outFilePath;
    }

    public void setOutFilePath(String outFilePath) {
        this.outFilePath = outFilePath;
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    @PostConstruct
    private void init(){
        overrideFromEnv();
    }

    private void overrideFromEnv() {

        if (StringUtils.isEmpty(drive)){
            drive = "MySql";
        }
        if (StringUtils.isEmpty(basePackage)){
            basePackage = "xyz.aiyouv";
        }
        if (StringUtils.isEmpty(entityPackage)){
            entityPackage = String.format("%s.%s", basePackage, "entity");
        }
        if (StringUtils.isEmpty(servicePackage)){
            servicePackage = String.format("%s.%s", basePackage, "service");
        }
        if (StringUtils.isEmpty(controllerPackage)){
            controllerPackage = String.format("%s.%s", basePackage, "controller");
        }
        if (StringUtils.isEmpty(repositoryPackage)){
            repositoryPackage = String.format("%s.%s", basePackage, "repository");
        }
        if (StringUtils.isEmpty(outFilePath)){
            outFilePath = "./";
        }

    }
}